<?php
namespace app\controller\api\cms;

use app\BaseController;
use app\model\cms\CategoryModel;
use app\model\cms\ArticleModel;
class Category extends BaseController
{


    public function index()
    {

        $list = CategoryModel::get_list();

        return json( $list );
    }

    /**
     * 该分类的兄弟分类
     */
    public function bro( $category_id ){
        // global $_GPC;
        // $id = $_GPC['id'] ? intval($_GPC['id']):0;
        // $category = CategoryModel::single( $category_id );
        $category = CategoryModel::where( 'id',$category_id )->find();
        $bro = CategoryModel::where( 'parent_id', $category->parent_id )->select();
        return json( $bro );
    }

    /**
     * 该分类下的文章列表
     */
    public function article($category_id){
        // global $_GPC;
        // $id = $_GPC['id'] ? intval($_GPC['id']):0;
        // $category = CategoryModel::single( $category_id );
        $category = CategoryModel::where( 'id',$category_id )->find();
        $article_list = ArticleModel::where( 'category_id', $category->id )->select();
        return json( $article_list );
    }

    /**
     * 该分类信息
     */
    public function info( $id )
    {

        // global $_GPC;
        // $id = $_GPC['id'] ? intval($_GPC['id']):0;
        $res = [
            'code'=>0
        ];
        if( !$id ){
            $res['code'] = 1;
            $res['message'] = '参数不合法';
        }
        $res['category'] = CategoryModel::single( $id );

        $res['category_path'] = CategoryModel::getParentsPath( $id );
        return json( $res );

    }

    public function tree()
    {

        $tree = CategoryModel::children( 0, true );

        return json( $tree );

    }

    

   

}